package com.charity.service.impl;

import com.charity.domain.User;
import com.charity.mapper.UserMapper;
import com.charity.service.IUserService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

@Slf4j
@Service
@Primary
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    @Transactional
    public boolean register(User user) {
        try {
            // 检查用户名是否已存在
            if (getByUsername(user.getUsername()) != null) {
                return false;
            }
            // 加密密码
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            // 设置默认角色
            user.setUserType(0);
            return userMapper.insert(user) > 0;
        } catch (Exception e) {
            log.error("用户注册异常{}", e);
            return false;
        }
    }

    @Override
    public User login(String username, String password) {
        try {
            User user = getByUsername(username);
            if (user != null && passwordEncoder.matches(password, user.getPassword())) {
                return user;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    @Autowired
    @Lazy
    private com.charity.security.CustomUserDetailsService customUserDetailsService;

    @Override
    public User adminLogin(String username, String password) {
        try {
            User user = getAdminByUsername(username);
            if (user != null && passwordEncoder.matches(password, user.getPassword())) {
                UserDetails userDetails = customUserDetailsService.createUserDetails(user);
                return user;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public User getAdminByUsername(String username) {
        try {
            return userMapper.selectAdminByUsername(username);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public User getByUsername(String username) {
        try {
            return userMapper.selectByUsername(username);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean updateUser(User user) {
        try {
            return userMapper.updateById(user) > 0;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Map<String, Object> getUsers(int page, int size, String username, Integer userType) {
        try {
            Map<String, Object> result = new HashMap<>();
            // 计算偏移量
            int offset = (page - 1) * size;
            // 获取总数
            int total = userMapper.selectCount(username, userType);
            // 获取列表数据
            List<User> users = userMapper.selectList(offset, size, username, userType);
            
            result.put("total", total);
            result.put("list", users);
            return result;
        } catch (Exception e) {
            log.error("获取用户列表异常: {}", e.getMessage());
            return new HashMap<>();
        }
    }
}